package com.huluobo.a2307monthdemo2.adapter

import android.view.View
import android.widget.Button
import android.widget.EditText
import android.widget.LinearLayout
import android.widget.TextView
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.blankj.utilcode.util.ToastUtils
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.huluobo.a2307monthdemo2.R
import com.huluobo.a2307monthdemo2.utils.ImageUtils
import com.huluobo.a2307monthdemo2.video.VideoFragment
import com.huluobo.a2307monthdemo2.video.VideoViewModel
import com.huluobo.lib_base.bean.CommentBean
import com.huluobo.lib_base.net.Const
import org.w3c.dom.Text

/**
 *  Created by LC on 2025/10/9.
 */
class CommentAdapter(
    private val viewModel: VideoViewModel,
    private val videoFragment: VideoFragment,
    private val videoId: Int
) : BaseQuickAdapter<CommentBean, BaseViewHolder>(R.layout.item_comment) {

    private val childCommentAdapter: ChildCommentAdapter = ChildCommentAdapter()
    override fun convert(holder: BaseViewHolder, item: CommentBean) {
        viewModel.getChildCommentSuccess.observe(videoFragment) {
            childCommentAdapter.data.clear()
            childCommentAdapter.data.addAll(it)
            childCommentAdapter.notifyDataSetChanged()
        }

        viewModel.responseCommentSuccess.observe(videoFragment) {
            ToastUtils.showLong("回复成功")
            //回复成功 关闭回复布局
            holder.getView<LinearLayout>(R.id.item_comment_response_ll).visibility = View.GONE
        }

        if (!item.icon.isNullOrEmpty()) {
            ImageUtils.loadImage(context, item.icon, holder.getView(R.id.item_comment_icon_iv))
        }
        holder.setText(R.id.item_comment_name_tv, item.username)
        holder.setText(R.id.item_comment_msg_tv, item.msg)
        holder.setText(R.id.item_comment_time_tv, item.createtime)

        if (item.childcount > 0) {
            holder.getView<TextView>(R.id.item_comment_more_tv).visibility = View.VISIBLE
        } else {
            holder.getView<TextView>(R.id.item_comment_more_tv).visibility = View.GONE
        }
        holder.getView<TextView>(R.id.item_comment_more_tv).setOnClickListener {
            viewModel.getChildComment(1, 10, item.rootid, videoId)
            it.visibility = View.GONE
            holder.getView<RecyclerView>(R.id.item_comment_more_rv).visibility = View.VISIBLE
        }
        //初始化二级列表
        holder.getView<RecyclerView>(R.id.item_comment_more_rv).layoutManager = LinearLayoutManager(context)
        holder.getView<RecyclerView>(R.id.item_comment_more_rv).adapter = childCommentAdapter

        //回复布局展示
        holder.getView<TextView>(R.id.item_comment_response_tv).setOnClickListener {
            holder.getView<LinearLayout>(R.id.item_comment_response_ll).visibility = View.VISIBLE
        }

        //回复按钮点击
        holder.getView<Button>(R.id.item_comment_send_btn).setOnClickListener {
            val msg = holder.getView<EditText>(R.id.item_comment_response_et).text.toString()
            if (msg.isNotEmpty()) {
                val map = mapOf(
                    Const.PARAM_MSG to msg,
                    Const.PARAM_PARENT_ID to item.id,
                    Const.PARAM_VIDEO_ID to videoId
                )
                //发送回复
                viewModel.responseComment(map)
            }
        }
    }
}